package cn.itcast.bos.service.base.impl;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService{
	@Resource
	private FixedAreaRepository repository;
	@Resource
	private CourierRepository courierRepository;
	@Resource
	private TakeTimeRepository takeTimeRepository;
	@Override
	public void save(FixedArea model) {
		repository.save(model);
	}
	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {
		return repository.findAll(specification, pageable);
	}
	@Override
	public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		//通过id分别查询快递员,定区,收派时间的对象
		FixedArea fixedArea = repository.findOne(model.getId());
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		//快递员关联到定区上
		fixedArea.getCouriers().add(courier);
		//收派时间关联到快递员上
		courier.setTakeTime(takeTime);
	}

}
